package com.hotelsystem.reservations.layouts;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;

import com.hotelsystem.reservations.persons.Client;
import com.hotelsystem.reservations.persons.ClientManagement;
import com.hotelsystem.reservations.rooms.Room;

public class UserListView {

	private JList<String> clientsList;
	private JButton open, edit, delete;
	private JPanel mainPanel;

	public UserListView() {
		buildUI();
	}

	public JPanel getMainPanel() {
		return mainPanel;
	}

	public JList<String> getClientsList() {
		return clientsList;
	}

	public JButton getOpen() {
		return open;
	}

	public JButton getEdit() {
		return edit;
	}

	public JButton getDelete() {
		return delete;
	}

	public void addListenerForOpen(ActionListener action) {
		open.addActionListener(action);
	}

	public void addListenerForEdit(ActionListener action) {
		edit.addActionListener(action);
	}

	public void addMouseListener(MouseListener listener) {
		clientsList.addMouseListener(listener);
	}
	
	public void addListenerForDelete(ActionListener action) {
		delete.addActionListener(action);
	}

	private void buildUI() {
		mainPanel = new JPanel(new BorderLayout());
		clientsList = new JList<>();
		mainPanel.add(clientsList, BorderLayout.CENTER);
		mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
		JPanel buttonsPanel = new JPanel(new BorderLayout());
		edit = new JButton("Edit");
		delete = new JButton("Delete");
		open = new JButton("Open");
		buttonsPanel.add(open, BorderLayout.WEST);
//		buttonsPanel.add(Box.createRigidArea(new Dimension(20, 0)));
//		buttonsPanel.add(edit, BorderLayout.CENTER);
//		buttonsPanel.add(Box.createRigidArea(new Dimension(20, 0)));
		buttonsPanel.add(delete, BorderLayout.EAST);
//		buttonsPanel.add(Box.createRigidArea(new Dimension(20, 0)));
		buttonsPanel.setBorder(BorderFactory.createEmptyBorder(20, 0, 0, 0));

		mainPanel.add(buttonsPanel, BorderLayout.SOUTH);
	}

	public void setClientList(ArrayList<Client> clients) {
		DefaultListModel<String> clientModels = new DefaultListModel<>();
		for(Client client : clients) {
			clientModels.addElement(client.getFirstName() + " " + client.getLastName());
		}
		this.clientsList.setModel(clientModels);
	}
	
	public static void main(String[] args) {
		UserListView layout = new UserListView();
		ClientManagement cm =new  ClientManagement();
		layout.setClientList(cm.getClients());
		JFrame frame = new JFrame();
		JPanel panel = layout.getMainPanel();
		frame.add(panel);
		frame.setSize(400, 200);
		frame.setVisible(true);
	}
}
